package com.lanou3g.musicenjoy.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.adapter.SearchingAlbumAdapter;
import com.lanou3g.musicenjoy.entity.SearchAlbum;
import com.lanou3g.musicenjoy.utils.NoScrollListView;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 辛鑫 on 15/10/21.
 * 专辑界面
 */
public class SearchingAlbumFragment extends Fragment implements AdapterView.OnItemClickListener {

    private TextView albumSongNum;
    private RelativeLayout searchAlbum;
    private NoScrollListView albumList;
    private List<SearchAlbum.DataEntity> data;
    private RequestQueue requestQueue;
    private String newUrl;
    private SearchingAlbumAdapter adapter;


    public static SearchingAlbumFragment passData(Context mContext, String str) {
        SearchingAlbumFragment fragment = new SearchingAlbumFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", str);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.searching_album_layout, null);
        albumSongNum = (TextView) view.findViewById(R.id.album_song_num);
        searchAlbum = (RelativeLayout) view.findViewById(R.id.search_album);
        albumList = (NoScrollListView) view.findViewById(R.id.album_list);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onResume() {
        super.onResume();
        requestQueue = VolleySingleton.getInstance(getActivity()).getRequestQueue();
        adapter = new SearchingAlbumAdapter(getActivity());
        albumList.setOnItemClickListener(this);
        Bundle bundle = getArguments();
        String url = bundle.getString("url");
        newUrl = "http://api.dongting.com/misc/search/album?uid=862136025609255&f=f3030&app=ttpod&hid=6143071666190381&rom=Coolpad%252FCoolpad8297W%252FCoolpad8297W%253A4.2.2%252FJDQ39%252F4.2.043.P0.141223.8297W%253Auser%252Frelease-keys&alf=alf702006&cpu=mt6582&resolution=720x1280&net=2&size=50&v=v8.2.0.2015091720&utdid=U157OqK3fvMBAPX2Buw9EsPo&s=s200&ram=2010676+kB&page=1&q=" + url + "&active=0&tid=0&mid=Coolpad%2B8297W&splus=4.2.2%252F17&cpu_model=MT6592";
        Log.i("searching_url", newUrl + "0000");
        getInternetData();
    }

    //从网络上拉取数据
    private void getInternetData() {
        StringRequest request = new StringRequest(newUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Gson gson = new Gson();
                SearchAlbum entity = gson.fromJson(s, SearchAlbum.class);
                if (entity != null && entity.getData() != null) {
                    data = entity.getData();
                    albumList.setAdapter(adapter);
                    adapter.setDatas(data);
                    albumSongNum.setText(data.size() + "");
                    searchAlbum.setVisibility(View.VISIBLE);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        requestQueue.add(request);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SearchAlbum.DataEntity dataEntity = (SearchAlbum.DataEntity) parent.getItemAtPosition(position);
        int urlId = dataEntity.getAlbumId();
        AlbumMessageFragment fragment = new AlbumMessageFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", urlId);
        fragment.setArguments(bundle);
        getFragmentManager()
                .beginTransaction()
                .addToBackStack(null)
                .replace(R.id.container, fragment)
                .commit();
    }

}
